package com.ph.order.api.controller;

import cm.ph.shopping.facade.order.entity.PhMerchantOrder;
import cm.ph.shopping.facade.order.service.IMerchantOrderService;
import cm.ph.shopping.facade.order.service.IPhManagerOrderAddressService;
import cm.ph.shopping.facade.order.vo.PhLogisticsVo;
import cm.ph.shopping.facade.order.vo.PhMerchantOrderListVo;
import cm.ph.shopping.facade.order.vo.QueryOrderDetailVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.agent.entity.Agent;
import com.ph.shopping.facade.agent.service.IAgentService;
import com.ph.shopping.facade.agent.vo.AgentVo;
import com.ph.shopping.facade.permission.vo.SessionRoleVo;
import com.ph.shopping.facade.permission.vo.SessionUserVo;
import com.ph.shopping.facade.supplier.entity.Supplier;
import com.ph.shopping.facade.supplier.service.ISupplierService;
import com.ph.shopping.facade.supplier.vo.SupplierVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 
 * @project：phshopping-api-platform
 * @description: 商户进货 
 * @author lichao  
 * @date 2017年3月23日
 */
@Controller
@RequestMapping("/api/merchantOrder")
public class MerchantOrderController extends BaseController {

	@Reference(version = "1.0.0")
	private IMerchantOrderService iMerchantOrderService;

	@Reference(version = "1.0.0")
	private ISupplierService iSupplierService;

	@Reference(version = "1.0.0")
	private IAgentService iAgentService;

    @Reference(version = "1.0.0")
    private IPhManagerOrderAddressService iPhManagerOrderAddressService;

	/**
	 * @param queryOrderDetailVo
	 * @return
	 * @description: 跳转至进货订单详情
	 * @author 舒豪
	 * @date 2017年3月23日
	 */
	@RequestMapping(value = "/orderEnterDetailPage", method = RequestMethod.GET)
	public Object toOrderEnterDetailPage(QueryOrderDetailVo queryOrderDetailVo) {
		try {
			ModelAndView mv = new ModelAndView("/merchant/order/orderEnterDetail");
            Result result = this.getOrderDetail(queryOrderDetailVo);
            mv.addObject("orderDetailVo", result.getData());
            return mv;
		} catch (Exception e) {
			logger.error("获取订单详情异常:", e.getMessage());
			return "";
		}

	}

	/**
	 * @param queryOrderDetailVo
	 * @return
	 * @description: 跳转至供货订单详情
	 * @author 舒豪
	 * @date 2017年3月23日
	 */
	@RequestMapping(value = "/orderSendDetailPage", method = RequestMethod.GET)
	public Object toOrderSendDetailPage(QueryOrderDetailVo queryOrderDetailVo) {
		try {
			ModelAndView mv = new ModelAndView("/merchant/order/orderSendDetail");
            Result result = this.getOrderDetail(queryOrderDetailVo);
            mv.addObject("orderDetailVo", result.getData());
            return mv;
		} catch (Exception e) {
			logger.error("获取订单详情异常:", e.getMessage());
			return "";
		}

	}

	/**
	 * @param queryOrderDetailVo
	 * @return
	 * @description: 跳转至平台订单详情
	 * @author 舒豪
	 * @date 2017年3月23日
	 */
	@RequestMapping(value = "/orderDetailPage", method = RequestMethod.GET)
	public Object toOrderDetailPage(QueryOrderDetailVo queryOrderDetailVo) {
		try {
			ModelAndView mv = new ModelAndView("/merchant/order/orderDetail");
            Result result = this.getOrderDetail(queryOrderDetailVo);
            mv.addObject("orderDetailVo", result.getData());
            return mv;
		} catch (Exception e) {
			logger.error("获取订单详情异常:", e.getMessage());
			return "";
		}

	}

	
	/**
	 * 
	 * @description: 跳转至进货单页面
	 * @param model
	 * @param ide 用于表格显示时的标识 根据入口来传值, 值为1表示进货单(只显示未审核)
	 * @return
	 * @author lichao
	 * @date 2017年3月23日
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String toMerchantOrderList(Model model, String ide){
		try{
			//获取供应商列表
			Supplier supplier = new Supplier();
			supplier.setStatus(1);
			List<SupplierVo> supplierList = iSupplierService.getSupplierVoList(supplier);
			logger.debug("供应商列表: " + supplierList);
			model.addAttribute("supplierList", supplierList);
			model.addAttribute("ide", ide == null ? "-1" : "1" );
			return "merchant/order/list";
		} catch (Exception ex) {
			logger.error("获取供应商列表异常, ex={}", ex);
			return "";
		}
		
	}
	
	/**
	 * 
	 * @description: 获取进货单列表分页 ajax
	 * @param pageBean
	 * @param phMerchantOrderListVo
	 * @return
	 * @throws Exception
	 * @author lichao
	 * @date 2017年3月23日
	 */
	@ResponseBody 
	@RequestMapping(value="/getMerchantOrderList",method=RequestMethod.GET)
	public Object getMerchantOrderList(Model model, HttpServletRequest request, PageBean pageBean, PhMerchantOrderListVo phMerchantOrderListVo) throws Exception{
		if(phMerchantOrderListVo.getIde().equals("1")){//只显示未审核
			phMerchantOrderListVo.setStatus("0");
		}
		//获取登录者信息
		SessionUserVo user =  getLoginUser(request);
		SessionRoleVo role =user.getSessionRoleVo().get(0);
		if(role.getRoleCode() == 6){
			//登录者是商户
			phMerchantOrderListVo.setMerchantId(user.getId());
			phMerchantOrderListVo.setPersonIde(1);
		}else if(role.getRoleCode() == 5){
			/*
			 * 如果登录者是区代理 会看见自己所对应的市代的供货单
			 * 获取对应的市级代理的id
			 */
			//自己
			Agent agent = new Agent();
			agent.setId(user.getId());
			AgentVo agentVo = iAgentService.getAgentDetail(agent);
			//县级
			Agent agentParent = new Agent();
			agentParent.setId(agentVo.getParentId());
			AgentVo agentVoParent = iAgentService.getAgentDetail(agentParent);
			//市级
			Agent agentCity = new Agent();
			agentCity.setId(agentVoParent.getParentId());
			AgentVo agentVoCity = iAgentService.getAgentDetail(agentCity);
			phMerchantOrderListVo.setAgentId(agentVoCity.getId());
			phMerchantOrderListVo.setPersonIde(3);//设置当前登录者级别
		}else if(role.getRoleCode() == 4){//查看是直接上级的市级代理
			/*
			 * 如果登录者是县级代理 会看见自己所对应的市代的供货单
			 * 获取对应的市级代理的id
			 */
			//自己
			Agent agent = new Agent();
			agent.setId(user.getId());
			AgentVo agentVo = iAgentService.getAgentDetail(agent);
			//县级
			Agent agentParent = new Agent();
			agentParent.setId(agentVo.getParentId());
			AgentVo agentVoParent = iAgentService.getAgentDetail(agentParent);
			phMerchantOrderListVo.setAgentId(agentVoParent.getId());
			phMerchantOrderListVo.setPersonIde(3);//设置当前登录者级别
		}else if(role.getRoleCode() == 3){//市级代理
			phMerchantOrderListVo.setAgentId(user.getId());
			phMerchantOrderListVo.setPersonIde(2);//设置当前登录者级别
		}else if(role.getRoleCode() == 1){
			//如果是管理员登录 去掉状态(status)查询条件即所有订单都会展示
			phMerchantOrderListVo.setIde("1");
			phMerchantOrderListVo.setPersonIde(4);//设置当前登录者级别
		}
		model.addAttribute("phMerchantOrderListVo", phMerchantOrderListVo);
		return iMerchantOrderService.getMerchantOrderList(pageBean, phMerchantOrderListVo);
	}


	/**
	 * @param queryOrderDetailVo
	 * @return
	 * @throws Exception
	 * @description: 获取订单详情 ajax
	 * @author 舒豪
	 * @date 2017年3月23日
	 */
	@ResponseBody
	@RequestMapping(value = "/getOrderDetail", method = RequestMethod.GET)
    public Result getOrderDetail(QueryOrderDetailVo queryOrderDetailVo) throws Exception {
        QueryOrderDetailVo queryOrderDetailVo1 = iMerchantOrderService.getOrderDetail(queryOrderDetailVo);
		if (queryOrderDetailVo1.getLogisticsId() != null) {
			PhLogisticsVo phLogisticsVo = iPhManagerOrderAddressService.findLogisticsById(queryOrderDetailVo1.getLogisticsId());
			if (phLogisticsVo != null) {
				queryOrderDetailVo1.setLogisticsCompany(phLogisticsVo.getLogisticsName());
			}
		}
		return getResult(ResultEnum.SUCCESS, queryOrderDetailVo1);
    }

	/**
	 * @param phMerchantOrder
	 * @return
	 * @throws Exception
     * @description: 修改物流费用
     * @author 舒豪
	 * @date 2017年3月23日
	 */
	@ResponseBody
	@RequestMapping(value = "/updateLogistics", method = RequestMethod.POST)
	public Object updateLogistics(PhMerchantOrder phMerchantOrder) throws Exception {
        SessionUserVo userVo = this.getLoginUser();//判断当前登录中是否是该订单的代理商
        if (userVo.getId().equals(phMerchantOrder.getAgentId())) {
            return iMerchantOrderService.updateLogistics(phMerchantOrder);
		}else{
			return getResult(ResultEnum.USER_ERROR);
        }
    }

    /**
     * @param phMerchantOrder
     * @return
     * @throws Exception
     * @description: 修改物流信息
     * @author 舒豪
     * @date 2017年3月23日
     */
    @ResponseBody
    @RequestMapping(value = "/updateLogisticsInfo", method = RequestMethod.POST)
    public Object updateLogisticsInfo(PhMerchantOrder phMerchantOrder) throws Exception {
        /*SessionUserVo userVo=this.getLoginUser();//判断当前登录中是否是该订单的代理商
		if(userVo.getId()==phMerchantOrder.getAgentId()){
		    return iMerchantOrderService.updateLogistics(phMerchantOrder);
		}else{
			return getResult(ResultEnum.USER_ERROR);
		}*/

        return iMerchantOrderService.updateLogisticsInfo(phMerchantOrder);

    }

}
